var addRepairListData = function (pageSize, pageNumber) {
    isNulltoWhat(pageSize, 8);
    isNulltoWhat(pageNumber, 1);
    var loadDiv = load("#body", 999999);
    $(".list-content").children("table").children("tbody").html("");
    $(".chooseNum").children("select").html("");
    $.ajax({
        url: "repairTask/list",
        type: "post",
        data: {
            "token": localStorage['token'],
            "pageSize": pageSize,
            "pageNumber": pageNumber
        },
        success: function (data) {
            console.log(data);
            if (data.code == 200) {
                console.log(data);
                listSetting(data.data.totalPages, data.data.pageNumber);
                var length = data.data.content.length;
                for (var j = 0; j < data.data.totalPages; j++) {
                    var option = addRealOption(j + 1);
                    if ((j + 1) == data.data.pageNumber) {
                        option.attr("selected", "selected");
                    }
                    option.appendTo($(".chooseNum").children("select"));
                }
                localStorage['pageSize'] = data.data.pageSize + "";
                localStorage['pageNumber'] = data.data.pageNumber + "";
                for (var i = 0; i < length; i++) {
                    var task = data.data.content[i];
                    var tr = $('<tr></tr>');
                    var state = stateChangeText(task.state);
                    addTd(task.device).appendTo(tr);
                    addTd(task.content).appendTo(tr);
                    addTd(task.dorm).appendTo(tr);
                    addTd(task.creator).appendTo(tr);
                    addTd(state).appendTo(tr);
                    addTd(formatTime(task.updateTime)).appendTo(tr);
                    var btnTd = $("<td></td>");
                    if (task.state == 0) {
                        // btnTd.text("<button class='redButton' onclick='' >不通过</button> <button  onclick=''>分配</button>");
                        $("<button class='redButton' onclick='changStateTest($(this))' >不通过</button> ").appendTo(btnTd);
                        $("<button  onclick='distribution($(this))'>分配</button>").appendTo(btnTd);
                    }
                    if (task.state == 4) {
                        $("<button class='button_cannot' disabled='disabled' onclick=''>已审核</button>").appendTo(btnTd);
                    }
                    if (task.state == 2) {
                        $("<button class='button_cannot' disabled='disabled' onclick=''>已分配</button>").appendTo(btnTd);
                    }
                    // console.log(btnTd);
                    btnTd.appendTo(tr);
                    addTdHidId(task.id).appendTo(tr);
                    tr.appendTo($(".list-content").children("table").children("tbody"))
                }
                $(".thisNum").children("span").text(data.data.pageNumber);
                // console.log(new Date().getSeconds() );
                if ($(".list-content").children("table").children("tbody").text() === "") {
                    var bk = getNothingBackground();
                    bk.appendTo($(".list-content").children("table").children("tbody"));
                }
            }
            load_close(loadDiv);
        }
    })
};

var stateChangeText = function (code) {
    var msg = "";
    if (code == 0) {
        msg = "审核中";
    } else if (code == 1) {
        msg = "分配任务中";
    } else if (code == 2) {
        msg = "处理中";
    } else if (code == 3) {
        msg = "已处理";
    } else if (code == 4) {
        msg = "未通过审核";
    }
    return msg;
};

var changeState = function (btn) {
    var loadDiv = load("#body", 99999);
    var id = btn.parent().next().text();
    $.ajax({
        url: "repairTask/unpass",
        type: "post",
        data: {
            "token": localStorage['token'],
            "id": id
        },
        success: function (data) {
            if (data.code == 200) {
                addRepairListData(8, 1);
            } else {
                errorMessage(data.msg);
            }
            load_close(loadDiv);
        }

    })
}
var repair_detail = function (btn) {
    var id = btn.parent().next().text();
    // console.log(id);
    var url = "/page/student/repairDetail";
    $("#body").load(url);
};


var changStateTest = function (btn) {
    swal(
        {
            title: "警告",
            text: "确定将状态修改为不通过吗?",
            type: "warning",
            buttons: ["取消", "确定！"],
            dangerMode: true
        }
    ).then((willDelete) => {
        if (willDelete) {
            changeState(btn);
        } else {
            swal("取消成功"
            )
            ;
        }
    })
};

var distribution = function (btn) {
    var id = btn.parent().next().text();
    dialog_show();
    $(".dialog").load("page/admin/distribution");
    localStorage['repairId'] = id;
    add_personal_data();
};

var add_personal_data = function () {
    $.ajax({
        url: "repair/",
        type: "post",
        data: {
            "token": localStorage['token'],
        },
        success: function (data) {
            // console.log(data);
            if (data.code == 200) {
                var contents = data.data;
                for (var i = 0; i < contents.length; i++) {
                    var content = contents[i];
                    var li = addOption(content.name, content.id);
                    li.appendTo($("#personal").next().next().next().children("ul"));
                }
            } else {
                errorMessage(data.msg);
            }
        }
    })

};

var disPersonalBtn = function () {
    var repairId = localStorage['repairId'];
    var personalId = $(".SHselect-father").children().next().next().val();
    // console.log(repairId);
    // console.log(personalId);
    localStorage['personId'] = personalId;

    $.ajax({
        url: "repairTask/distri",
        type: "post",
        data: {
            "token": localStorage['token'],
            "repairId": repairId,
            "personalId": personalId,
        },
        success: function (data) {
            if (data.code == 200) {
                message("请求成功", "", "success", 1500);
                notify();

            } else {
                errorMessage(data.msg);
            }
            localStorage.removeItem("repairId");
        }
    })
};


var notify = function (btn) {
    swal(
        {
            title: "提示",
            text: "是否短信通知该人员?",
            type: "warning",
            buttons: ["取消", "确定"],
            dangerMode: true
        }
    ).then((willDelete) => {
        if (willDelete) {
            $.ajax({
                url: "repairTask/notify",
                type: "post",
                data: {
                    "token": localStorage['token'],
                    "id": localStorage['personId']
                },
                success: function (data) {
                    if (data.code == 200) {
                        swal("已短信通知!")
                    } else {
                        message("错误", data.msg, "error", 1000);
                    }
                }
            });
        } else {
        }
        addRepairListData(8, 1);
        setTimeout(function () {
            dialog_close();
        }, 500);
    })
};

$("#next").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) + 1;
        addRepairListData(8, pageNumber);
    }

});

$("#last").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) - 1;
        addRepairListData(localStorage['pageSize'], pageNumber);
    }

});


$(".chooseNum").children("select").change(function () {
    addRepairListData(localStorage['pageSize'], $(this).val());
});
